import page
import time
import datetime
import sys
from selenium import webdriver
from selenium.webdriver.common.by import By

def func(urlname="market", section="西区教超", start_time=None, confirm=False):
    
    br = page.Browser()
    br.url_get("https://dailyreport.sjtu.edu.cn/"
               + urlname +"/#/")
    # br.url_get("https://dailyreport.sjtu.edu.cn/haircut/#/")
    # br.url_get("https://dailyreport.sjtu.edu.cn/market/#/")
    
    time.sleep(5)
    # 指定时间之前脚本进行睡眠
    if start_time == None:
        pass
    else:
        while True:
            time_now = datetime.datetime.now()
            hour_now = int(time_now.strftime("%H"))
            min_now = int(time_now.strftime("%M"))
            sec_now = int(time_now.strftime("%S"))
            print("time_now:", hour_now, min_now,
                  sec_now, " start_time:", start_time)
            if hour_now >= start_time[0] and min_now >= start_time[1] and sec_now >= start_time[2]:
                break
            else:
                time.sleep(5)
            
    # br.select_section("西区教育超市", "", 0.01)
    br.select_section(section, "")
    br.reserve()
    
    if confirm:
        br.confirm()
        
        ## 如果出错的时候能关掉提示框重新点确认
        while True:
            try:
                errbtn = br.browser.find_element(by=By.XPATH,
                                                 value="//button[contains(text(),'确定') and contains(@class,'mint-msgbox-btn')]")
                print("errbtn.is_displayed:", errbtn.is_displayed())
                print("errbtn.is_enabled:", errbtn.is_enabled())
                if errbtn.is_displayed() and errbtn.is_enabled():
                    print("see btnerr")
                    errbtn.click()
                    br.confirm()
                
                    
            except:
                print("except #1")
                pass
        
        
        # while True:
        #     time.sleep(0.01)
        #     try:
        #         errhint = br.browser.find_element(by=By.XPATH,
        #                                         value="//div[contains(text(),'提示') and contains(@class,'mint-msgbox-title')]")
        #         print("hint msgbox")
        #         if errhint.is_displayed():
        #             print("hint msgbox displayed")
        #             # 说明发现了提示框
        #             # 按掉提示框的确定按钮，然后继续按确认
        #             btnerr = br.browser.find_element(by=By.XPATH,
        #                                             value="//button[contains(text(),'确定') and contains(@class,'mint-msgbox-btn')]")
        #             print("queren hint")
        #             btnerr.click()
        #             # br.refresh() # 应该不需要刷新
        #             br.confirm()
        #         else:
        #             print("not displayed")
        #     except:
        #         print("suc")
        #         br.confirm() ##  调试用，以防外一
        #         pass
    
    input()


if __name__ == "__main__":
    
    
    print(sys.argv)
    
    argv = sys.argv
    
    if len(argv) == 1:
        func("market", "西区教超", None, False)
    
    if len(argv) == 2:
        func(argv[1], start_time=None, confirm=False)
    
    if len(argv) == 3:
        func(argv[1], argv[2], start_time=None, confirm=False)
    
    if len(argv) == 4:
        func(argv[1], argv[2], start_time=None, 
             confirm=(False if argv[3]=="0" else True))
    
    if len(argv) == 5:
        sys.stdout = open(argv[4], 'w', encoding=sys.getfilesystemencoding())
        func(argv[1], argv[2], start_time=None, 
             confirm=(False if argv[3]=="0" else True))
    
    
    
    